<?php
/**
 * 逻辑区：广告系统
 * 
 * @author fengwei
 * @package logic
 * @name ad.logic.php
 * @version 1.0
 */

class AdLogic{
	public function GetList($city_id=0){
		$sql_limit_city = '1';
		if ($city_id>0){
			$sql_limit_city = 'city_id = '.$city_id.' ';
		}
		
		$sql='SELECT * from '.TABLE_PREFIX.'tttuangou_ad where '.$sql_limit_city.' ORDER BY ad_id desc';
		$sql = page_moyo($sql);
		$ad_list = dbc(DBCMax)->query($sql)->done();
		return $ad_list;
	}
	
	public function getOne($id=0){
		if($id){
			$sql = 'SELECT * from  '.table('ad').' where ad_id='.$id;
			$ad = dbc(DBCMax)->Query($sql)->limit(1)->done();
			return $ad;
		}else{
			return 0;
		}
	}
	
	public function add($data=''){
		if($data==''){
			return 0;
		}else{
			dbc()->SetTable(table('ad'));
			return dbc()->Insert($data);
		}
	}
	
	public function GetPositionList(){
		$sql='SELECT * from '.TABLE_PREFIX.'tttuangou_ad_position  ORDER BY position_id desc';
		$sql = page_moyo($sql);
		$position_list = dbc(DBCMax)->query($sql)->done();
		return $position_list;
	}
	
	public function add_position($data=''){
		if($data==''){
			return 0;
		}else{
			dbc()->SetTable(table('ad_position'));
			return dbc()->Insert($data);
		}
	}
	
	public function getOnePosition($id=0){
		if($id){
			$sql = 'SELECT * from  '.table('ad_position').' where position_id='.$id;
			$position = dbc(DBCMax)->Query($sql)->limit(1)->done();
			return $position;
		}else{
			return 0;
		}
	}
	
	
	public function getPositionName($id=0){
		if($id){
			$sql = 'SELECT position_name from  '.table('ad_position').' where position_id='.$id;
			$position = dbc(DBCMax)->Query($sql)->limit(1)->done();
			return $position['position_name'];
		}else{
			return '未知';
		}
	}
	
	public function getAd($position_name="",$default=0,$num=1){
		$temp = '';
		if($position_name){
			$city = logic('misc')->City();		
			$city_id = $city['cityid'];
			$sql = "SELECT a.*,p.position_style from  ".table('ad')." as a INNER JOIN ".table('ad_position')." as p ON a.position_id=p.position_id where p.position_name='".$position_name."' and a.city_id='".$city_id."' and a.enabled='1' and a.start_time<'".time()."' and a.end_time>'".time()."'  order by a.ad_id desc ";
			$ad = dbc(DBCMax)->Query($sql)->limit($num)->done();
			if (!$ad) {
				if ($default) {
					include handler('template')->file('@html/ad/ad_'.$default);
				}else{
					return $temp;
				}
				
			}else{
				if ($num>1) {
					include handler('template')->file('@html/ad/ad_'.$ad[0]['position_style']);
				}else{
					include handler('template')->file('@html/ad/ad_'.$ad['position_style']);
				}
				
			
			}
			
			
			
		}else{
			return $temp;
		}
	}
	
	
	
	private function parse_php($content){
		if(empty($content)) return false;
		$content = preg_replace("/{(.+?)}/is", "<?php $1 ?>", $content);
	
		return $content;
	}
	
	private function parse_comm($content) {
		$search = array(
				"/{\/foreach}/is",
				"/{\/if}/is",
				"/{else}/is",
	
		);
	
		$replace = array(
				"<?php } ?>",
				"<?php } ?>",
				"<?php } else { ?>"
		);
		$content = preg_replace($search, $replace, $content);
		return $content;
	}
	
	
	
	
	
	
	
	
	
	
	
}